home *** CD-ROM | disk | FTP | other *** search
- Path: centre.univ-orleans.fr!desiree!emmguyot
- From: emmguyot@desiree.univ-orleans.fr (Emmanuel GUYOT)
- Newsgroups: comp.lang.c
- Subject: Re: Loading a structure from a file
- Date: 4 Jan 1996 09:38:07 GMT
- Organization: CITU - Universite d'Orleans - FRANCE
- Message-ID: <4cg75v$l2j@centre.univ-orleans.fr>
- References: <4cfg1d$5n9@vector.wantree.com.au>
- NNTP-Posting-Host: desiree.cnrs-orleans.fr
- X-Newsreader: TIN [version 1.2 PL2]
-
- Jody Petroni (jpet@wantree.com.au) wrote:
- : Im trying to load this large structure from a file=>
-
- : /*Existing Job Record*/
- : struct jobrec{
- : int job;
- : char desc[81];
- : int amt[50];
- : };struct jobrec jobrecord[100];
-
- : rather than the following code=>
-
- : while(fscanf(fleptr,"%d%s%d%d%d...(50 times)",jobrecord[x].job,
- : jobrecord[x].desc,jobrecord[x].amt[y]..(50 times)..)!=EOF){
- : /*... Other code here*/
- : }
- : Is there an easier way to do this
-
- If you don't bother about data portability, you can do this :
-
- To write the structure :
-
- fwrite (&(jobrecord[x]), sizeof(struct jobrec), 1, fleptr);
-
- To read it :
-
- fread (&(jobrecord[x]), sizeof(struct jobrec), 1, fleptr);
-
- This works fine. The only problem is that depending on the compiler
- and on the computer, some padding bytes may have been added.
- Anyway, if you write and read the data on the same computer
- and both program where compiled with the same compiler,
- there won't be any problem.
-
- E.G.
-
- ------------------------------------------------------------------------------
- ---------------------------->>>> Emmanuel Guyot <<<<--------------------------
- LPCE-CNRS |
- 3A avenue de la Recherche Scientifique | Phone : 33 38 51 78 25
- 45071 Orleans Cedex 2 | Fax : 33 38 63 12 34
- France | EMail : emmguyot@cnrs-orleans.fr
- ------------------------------------------------------------------------------
- Home Page : http://desiree.cnrs-orleans.fr/cgi-bin/cpt_html?index
- ------------------------------------------------------------------------------
-